package com.hmyd.hmyd_android.dialog;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.RegPatterns;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;


public class ChangeNicknameDialog extends Dialog implements
		android.view.View.OnClickListener {
	
	private Context mContext;

	private View mClosed;
	private EditText name;
	private Button ok;

	private ChangeNicknameDialogListener mOnclickListener;
	
	public ChangeNicknameDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

	}

	public ChangeNicknameDialog(Context context, int theme,ChangeNicknameDialogListener onclickListener) {
		super(context, theme);
		mContext = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mOnclickListener = onclickListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_change_nickname);

		mClosed = findViewById(R.id.closed);
		mClosed.setOnClickListener(this);
		
		name = (EditText) findViewById(R.id.name);
		
		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.closed:
			cancel();
			break;
		case R.id.ok:
			if(TextUtils.isEmpty(name.getText().toString().trim())){
				MyToast.showToast(mContext, "昵称不能为空");
				
				return;
			}
			
			if(!RegPatterns.checkNicknameLength(name.getText().toString().trim())){
				MyToast.showToast(mContext, "请输入2-7个汉字或4-15位中英文");
				
				return;
			}
			
			if(!RegPatterns.checkNicknameChars(name.getText().toString().trim())){
				MyToast.showToast(mContext, "昵称允许使用中英文、数字、下划线(_)或减号(-)");
				
				return;
			}
			
			
			mOnclickListener.getIdentifyind(name.getText().toString().trim());
			cancel();
			
			break;
		default:
			break;
		}

	}

}
